<template>
    <div class="list-container">
        <div class='search-area'>
            <div class='search-left'>
                <div class='input-line'>
                    <span class='label'>搜索关键词：</span>
                    <el-input placeholder="请输入商户名" label='商户名' v-model="searchParams.keyword"/>
                </div>
            </div>
            <div class='search-right'>
                <el-button type="primary" @click='onSearchClick'>查询</el-button>
                <el-button type="primary" @click='openDialog'>新增协议</el-button>
            </div>
        </div>
        <el-table
            fit
            :data="tableData"
            style="width: 100%">
            <el-table-column
                prop="title"
                label="协议标题（协议名称）">
            </el-table-column>
            <el-table-column
                prop="languageType"
                label="语言类型">
                <template slot-scope="scope">
                    <el-tag v-if="scope.row.languageType === 'ch'" type="warning">中文</el-tag>
                    <el-tag v-if="scope.row.languageType === 'en'" type="danger">英文</el-tag>
                    <el-tag v-if="scope.row.languageType === 'fan'" type="success">繁体</el-tag>
                </template>
            </el-table-column>
            <el-table-column
                prop="content"
                label="协议文章详细介绍">
                <template slot-scope="scope">
                    <el-button type='primary' size='mini' @click='onDetailPreviewClick(scope.row.content)'>查看详细介绍</el-button>
                </template>
            </el-table-column>

            <el-table-column
                align='center'
                label="操作"
                width='200'>
                <template slot-scope="scope">
                    <div style='display: flex;align-items: center;flex-wrap: wrap'>
                        <el-button type='primary' size='mini' @click='onDeleteClick(scope.row.id)'>删除</el-button>
                        <el-button type='primary' size='mini' @click='handleModify(scope.row.id)'>修改</el-button>
                    </div>
                </template>
            </el-table-column>
        </el-table>
        <div class='pagination'>
            <el-pagination
                background
                layout="prev, pager, next"
                :current-page='currentPage'
                :page-size='10'
                :total="total"
                @current-change='onPageChange'
            >
            </el-pagination>
        </div>

        <!--   新增文章弹框     -->
        <el-dialog :title="dialogTitle" :visible.sync="dialogVisible" :before-close="onDialogClose()">
            <el-form ref="dataFormRef" :rules="rules" :model="addForm" label-width="80px" v-if="dialogVisible">
                <el-form-item label="语言类型" prop="status">
                    <el-radio-group v-model="addForm.languageType">
                        <el-radio-button label="ch">中文</el-radio-button>
                        <el-radio-button label="en">英文</el-radio-button>
                        <el-radio-button label="fan">繁体</el-radio-button>
                    </el-radio-group>
                    <div class="upload-notice">协议文章相关内容，请所选择的语言类型！</div>
                </el-form-item>
                <el-form-item label="协议标题" prop="title">
                    <el-input v-model="addForm.title" placeholder="请输入安卓协议文章链接"></el-input>
                </el-form-item>
                <el-form-item label="协议文章详情" prop="content">
                    <!-- 工具栏 -->
                    <Toolbar
                        style="border-bottom: 1px solid #ccc"
                        :editor="editor"
                        :defaultConfig="toolbarConfig"
                    />
                    <!-- 编辑器 -->
                    <Editor
                        style="height: 400px; overflow-y: hidden"
                        :defaultConfig="editorConfig"
                        v-model="addForm.content"
                        @onChange="onChange"
                        @onCreated="onCreated"
                    />
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogVisible = false">取 消</el-button>
                <el-button type="primary" @click="onDialogSubmit('modify')" v-if="addForm.id">确认修改</el-button>
                <el-button type="primary" @click="onDialogSubmit('add')" v-else>立即创建</el-button>
            </div>
        </el-dialog>

        <!--   协议文章富文本详情预览     -->
        <el-dialog title="详细介绍预览" :visible.sync="detailTextDialogVisible" @close="onDialogClose()">
            <div class="detail-text-preview" v-html="currentDetailText"></div>
            <div slot="footer" class="dialog-footer">
                <el-button type="primary" @click="detailTextDialogVisible = false">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
    import { getDocumentList, addDocument, deleteDocument, updateDocument, documentDetail } from '@/api/document'
    import { uploadFiles } from '@/api/common'
    import { Editor, Toolbar } from "@wangeditor/editor-for-vue";

    export default {
        components: { Editor, Toolbar },
        data() {
            return {
                editor: null,
                editorConfig: {
                    placeholder: "请输入内容",
                    // autoFocus: false,
                    // readOnly: true, // 只读、不可编辑
                    // 所有的菜单配置，都要在 MENU_CONF 属性下
                    MENU_CONF: {
                        // 配置上传图片
                        uploadImage: {
                            customUpload: this.uploadImgForEditor
                        },
                    },
                },
                toolbarConfig: {
                    /* 显示哪些菜单，如何排序、分组 */
                    toolbarKeys: [
                        'headerSelect',
                        // '|',
                        'bold',
                        'underline',
                        'italic',
                        'color',
                        'bgColor',
                        // '|',
                        'indent',  // 增加缩进
                        'delIndent',  // 减少缩进
                        'justifyLeft',  // 左对齐
                        'justifyRight',  // 右对齐
                        'justifyCenter',  // 居中
                        'justifyJustify',  // 两端对齐
                        // '|',
                        'fontSize',
                        'fontFamily',
                        'lineHeight',
                        // '|',
                        'bulletedList',
                        'numberedList',
                        'todo',
                        'insertLink',
                        'insertTable',
                        // '|',
                        'codeBlock',
                        'divider',
                        'uploadImage',
                        'undo',
                        'redo',
                    ],
                    // excludeKeys: [ ], /* 隐藏哪些菜单 */
                },
                editorConfig: {
                    placeholder: "请输入内容",
                    // autoFocus: false,
                    // readOnly: true, // 只读、不可编辑
                    // 所有的菜单配置，都要在 MENU_CONF 属性下
                    MENU_CONF: {
                        // 配置上传图片
                        uploadImage: {
                            customUpload: this.uploadImgForEditor
                        },
                    },
                },
                searchParams: {
                    pageSize: 10,
                    keyword: '', // 关键词搜索
                },
                currentPage: 1,
                total: 0,
                tableData: [],
                dialogVisible: false,
                addForm: {
                    id: '',
                    title	: '', // 协议标题
                    content: '', // 协议内容
                    languageType: 'ch', // 语言类型 en:英文 ch:中文
                },
                rules: {
                    id: [
                        {
                            required: false,
                            message: '',
                            trigger: 'blur'
                        },
                    ],
                    title: [
                        {
                            required: true,
                            message: '请填写协议标题',
                            trigger: 'blur'
                        },
                    ],
                    languageType: [
                        {
                            required: true,
                            message: '请选择语言类型',
                            trigger: 'blur'
                        },
                    ],
                    content: [
                        {
                            required: true,
                            message: '请填写APP产品详情介绍',
                            trigger: 'blur'
                        },
                    ],
                },
                dialogTitle: '新增协议文章',
                detailTextDialogVisible: false, // 协议文章详情介绍预览弹框
                currentDetailText: '', // 协议文章详情介绍预览数据
                carouselPicList: [], // 协议文章主图列表
                thumbnailPicList: [], // 缩略图列表
            }
        },
        mounted() {
            this.getArticleListFn()
        },
        methods: {
            onCreated(editor) {
                this.editor = Object.seal(editor);
            },
            onChange(editor) {
                this.$emit('changeData', this.html)
            },

            // 富文本编辑器上传图片
            async uploadImgForEditor(file, insertFn) {
                let formData = new FormData()
                formData.append('file', file)
                let res = await uploadFiles(formData, {  })
                const { url } = res
                insertFn(url)
            },

            onPageChange(page) {
                this.currentPage = page
            },
            onSearchClick() {
                this.getArticleListFn(1)
            },

            // 获取协议文章列表数据
            async getArticleListFn(currentPage) {
                let requestParams = this.searchParams
                console.log(requestParams ,this.searchParams, 'MMMMMM')
                requestParams.pageIndex = currentPage || this.currentPage
                const data = await getDocumentList(requestParams)
                this.total = +data.total
                this.currentPage = +data.pageIndex || 1
                this.tableData = data.list
            },

            // 显示协议文章添加弹框
            openDialog() {
                this.dialogVisible=true
            },

            // 关闭协议文章添加弹窗
            onDialogClose() {
                // this.$refs.dataFormRef && this.$refs.dataFormRef.resetFields()
            },

            // 协议文章添加弹框提交
            onDialogSubmit(type) {
                if (type === 'modify') {
                    this.modifyArticleFn()
                } else {
                    this.addArticleFn()
                }
            },

            // 添加协议文章
            async addArticleFn() {
                let requestParams = this.addForm
                const data = await addDocument(requestParams)
                this.dialogVisible = false
                this.$message.success('协议文章添加成功')
                this.getArticleListFn(1)
                window.location.reload()
            },

            // 修改协议文章
            async modifyArticleFn() {
                let requestParams = this.addForm
                const data = await updateDocument(requestParams)
                this.dialogVisible = false
                this.$message.success('协议文章修改成功')
                this.getArticleListFn(1)
                window.location.reload()
            },

            // 删除协议文章
            onDeleteClick(id) {
                this.$confirm('此操作将永久删除该协议文章, 是否继续?', '提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(async () => {
                    const data = await deleteDocument({id})
                    this.$message.success('删除成功')
                    this.getArticleListFn(1)
                })
            },

            // 协议文章编辑
            async handleModify(id) {
                this.dialogVisible = true

                let requestParams = {id}
                let res = await documentDetail(requestParams)

                this.addForm.id = res.id
                this.addForm.title = res.title
                this.addForm.languageType = res.languageType
                this.addForm.content = res.content
            },

            // 查看协议文章富文本信息
            onDetailPreviewClick(detailText) {
                this.currentDetailText = detailText
                this.detailTextDialogVisible = true
            }
        }
    }
</script>
<style lang='scss'>
    .list-container{
        .thumbnail{
            width: 100px;
        }
        .detail-text-preview{
            img{
                max-width: 100%;
            }
        }
        .search-area{
            display: flex;
            align-items: center;
            width: 100%;
            padding: 20px 0;
            border-bottom: 1px solid #f2f2f2;
            .search-left{
                display: flex;
                align-items: center;
                justify-content: center;
                .input-line{
                    display: flex;
                    align-items: center;
                    justify-content: center;
                    margin-right: 20px;
                    .label{
                        flex-shrink: 0;
                    }
                    .gap{
                        padding: 0 10px;
                    }
                }
            }
            .search-right{
                display: flex;
                justify-content: flex-end;
                margin-left: 20px;
            }
        }
        .pagination{
            margin-top: 20px;
        }

        .upload-notice{
            color: red;
        }
    }
</style>
